﻿using ProjectCloth.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class ProductListFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public ProductListFrm()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            //dataGridView1.DataSource = (from t in db.Products.Where(o => o.Enabled == true)
            //                            select new
            //                            {
            //                                t.Enabled,
            //                                t.CreateTime,
            //                                t.Name,
            //                                t.ProductNo,
            //                                t.Price,
            //                                t.BatchPrice,
            //                                t.Assortment,
            //                                t.ColorType,
            //                                ProductId = t.Id
            //                            }).ToList();
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            var productTxtStr = string.IsNullOrWhiteSpace(productNoTxt.Text) ? "" : productNoTxt.Text;
            dataGridView1.DataSource = (from t in db.Products.AsNoTracking().Where(o => o.Enabled == true && (productTxtStr == "" || o.ProductNo.Contains(productTxtStr.ToUpper())))
                                        select new
                                        {
                                            t.Enabled,
                                            t.CreateTime,
                                            //t.Name,
                                            t.ProductNo,
                                            t.Price,
                                            t.BatchPrice,
                                            //t.Assortment,
                                            //t.ColorType,
                                            ProductId = t.Id
                                        }).ToList();
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                #region  
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn)
                {
                    var productId = dataGridView1.CurrentRow.Cells["ProductId"].Value;
                    if (productId != null && (int)productId > 0)
                    {
                        if (column.Name == "EditBtn")
                        {
                            string productNo = dataGridView1.CurrentRow.Cells["ProductNo"].Value.ToString();
                            if (!string.IsNullOrEmpty(productNo))
                            {
                                var product = new ProductFrm(productNo);
                                var result = product.ShowDialog();
                                if (result.Equals(DialogResult.Cancel))
                                {
                                    searchBtn_Click(null, null);
                                }

                            }
                        }

                        else if (column.Name == "DeleteBtn")
                        {
                            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                            DialogResult dr = MessageBox.Show("确定要删除该商品吗?", "删除该商品", messButton);
                            if (dr == DialogResult.OK)//如果点击“确定”按钮
                            {
                                db.Products.Find((int)productId).Enabled = false;
                                if (db.SaveChanges() > 0)
                                {
                                    MessageBox.Show("删除成功！");
                                    searchBtn_Click(null, null);
                                }
                                else
                                {
                                    MessageBox.Show("删除失败！");
                                }

                            }

                        }
                    }
                }
                #endregion
            }
        }     
    }
}
